package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.service.ICartOrderService;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: MH
 * @Date: 2022-08-25 20:06
 * @Description: 下单后清除购物车中已经下单的商品
 */
@Service
public class ICartOrderServiceImpl implements ICartOrderService {

    @Autowired
    RedissonClient redissonClient;

    @Override
    public void clearCartItemByUserID(ClearCartItemRequest request) {
        @NotNull Long userId = request.getUserId();
        @NotEmpty List<Long> productIds = request.getProductIds();
        //获取Redis
        String userCartName = "Cart" + userId;
        RMap<Long, CartProductDto> map = redissonClient.getMap(userCartName);
        for (Long productId : productIds) {
            map.remove(productId);
        }
    }
}
